package com.netposa.service.impl;

import com.netposa.mapper.db0.GpsMapper;
import com.netposa.model.GpsModel;
import com.netposa.service.GpsService;
import com.netposa.toolkit.Utils;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.nio.ch.DirectBuffer;

@Service
public class GpsServceImpl implements GpsService {
   private static final Charset defaultEncoding = Charset.forName("UTF-8");
   private static Logger logger = Logger.getLogger(GpsServceImpl.class);
   @Value("${netposa.tidedb.ip}")
   private String tideDbIp;
   @Value("${netposa.tidedb.port}")
   private Integer tideDbPort;
   @Value("${netposa.tidedb.expand}")
   private String gpsExpand;
   @Autowired
   private GpsMapper gpsMapper;

   public List getGpsInfo() {
      List gpsModels = this.gpsMapper.getGpsInfo();
      if (!CollectionUtils.isEmpty(gpsModels)) {
         Iterator var2 = gpsModels.iterator();

         while(var2.hasNext()) {
            GpsModel gpsModel = (GpsModel)var2.next();
            long timeStamp = System.currentTimeMillis();
            String formatTime = Utils.getFormatTime(timeStamp, "yyyyMMddhhmmss");
            gpsModel.setGpsTime(formatTime);
            gpsModel.setGpsExpand(this.gpsExpand);
         }
      }

      return gpsModels;
   }

   public byte[] getTideDBGpsBytes(List gpsModelList) {
      byte[] packageBytes = null;

      try {
         if (gpsModelList == null || gpsModelList.size() == 0) {
            return packageBytes;
         }

         int packageLength = 10;
         List dateBufferList = new ArrayList();
         Iterator var5 = gpsModelList.iterator();

         ByteBuffer dataBuffer;
         byte[] gpsTime;
         while(var5.hasNext()) {
            GpsModel gpsModel = (GpsModel)var5.next();
            dataBuffer = null;
            if (!StringUtils.isEmpty(gpsModel.getGpsId())) {
               byte[] gpsId = gpsModel.getGpsId().getBytes(defaultEncoding);
               gpsTime = null;
               if (StringUtils.isNotEmpty(gpsModel.getGpsTime())) {
                  gpsTime = gpsModel.getGpsTime().getBytes(defaultEncoding);
               }

               byte[] gpsLati = null;
               if (StringUtils.isNotEmpty(gpsModel.getGpsLati())) {
                  gpsLati = gpsModel.getGpsLati().getBytes(defaultEncoding);
               }

               byte[] gpsLongi = null;
               if (StringUtils.isNotEmpty(gpsModel.getGpsLongi())) {
                  gpsLongi = gpsModel.getGpsLongi().getBytes(defaultEncoding);
               }

               byte[] gpsExpand = null;
               if (StringUtils.isNotEmpty(gpsModel.getGpsExpand())) {
                  gpsExpand = gpsModel.getGpsExpand().getBytes(defaultEncoding);
               }

               int gpsIdLength = gpsId == null ? 0 : gpsId.length;
               int gpsTimeLength = gpsTime == null ? 0 : gpsTime.length;
               int gpsLatiLength = gpsLati == null ? 0 : gpsLati.length;
               int gpsLongiLength = gpsLongi == null ? 0 : gpsLongi.length;
               int gpsExpandLength = gpsExpand == null ? 0 : gpsExpand.length;
               int dataLength = 29 + gpsIdLength + gpsTimeLength + gpsLatiLength + gpsLongiLength + gpsExpandLength;
               dataBuffer = ByteBuffer.allocateDirect(dataLength);
               dataBuffer.clear();
               dataBuffer.putInt(dataLength - 4);
               dataBuffer.put(Utils.hexStringToBytes("01"));
               dataBuffer.putInt(gpsIdLength);
               dataBuffer.put(gpsId);
               dataBuffer.put(Utils.hexStringToBytes("02"));
               dataBuffer.putInt(gpsTimeLength);
               dataBuffer.put(gpsTime);
               dataBuffer.put(Utils.hexStringToBytes("03"));
               dataBuffer.putInt(gpsLatiLength);
               dataBuffer.put(gpsLati);
               dataBuffer.put(Utils.hexStringToBytes("04"));
               dataBuffer.putInt(gpsLongiLength);
               dataBuffer.put(gpsLongi);
               dataBuffer.put(Utils.hexStringToBytes("05"));
               dataBuffer.putInt(gpsExpandLength);
               dataBuffer.put(gpsExpand);
               dateBufferList.add(dataBuffer);
               packageLength += dataLength;
            }
         }

         ByteBuffer packageBuffer = ByteBuffer.allocateDirect(packageLength);
         packageBuffer.clear();
         packageBuffer.put(Utils.hexStringToBytes("01"));
         packageBuffer.putInt(gpsModelList.size());
         packageBuffer.put(Utils.hexStringToBytes("00"));
         packageBuffer.putInt(packageLength - 10);
         Iterator var21 = dateBufferList.iterator();

         while(var21.hasNext()) {
            dataBuffer = (ByteBuffer)var21.next();
            dataBuffer.flip();
            gpsTime = new byte[dataBuffer.capacity()];
            dataBuffer.get(gpsTime);
            packageBuffer.put(gpsTime);
            ((DirectBuffer)dataBuffer).cleaner().clean();
         }

         packageBuffer.flip();
         packageBytes = new byte[packageLength];
         packageBuffer.get(packageBytes);
         ((DirectBuffer)packageBuffer).cleaner().clean();
      } catch (Exception var19) {
         logger.error(var19.getMessage(), var19);
      }

      return packageBytes;
   }

   public void sendGpsToTideDB(byte[] gpsBytes) {
      Socket tideSocketClient = null;
      OutputStream outputStream = null;
      InputStream inputStream = null;

      try {
         if (gpsBytes != null && gpsBytes.length != 0) {
            tideSocketClient = new Socket(this.tideDbIp, this.tideDbPort);
            outputStream = tideSocketClient.getOutputStream();
            outputStream.write(gpsBytes);
            outputStream.flush();
            byte[] respHeadBytes = new byte[10];
            inputStream = tideSocketClient.getInputStream();
            inputStream.read(respHeadBytes);
            ByteBuffer respHeadBuffer = ByteBuffer.allocateDirect(10);
            respHeadBuffer.clear();
            respHeadBuffer.put(respHeadBytes);
            respHeadBuffer.flip();
            respHeadBuffer.get();
            int count = respHeadBuffer.getInt();
            String status = respHeadBuffer.get() + "";
            int respBodyLength = respHeadBuffer.getInt();
            byte[] respBodyBytes = new byte[respBodyLength];
            inputStream.read(respBodyBytes);
            String respBody = new String(respBodyBytes, defaultEncoding);
            logger.info("GPS写入数据条数：" + count + ",返回状态：" + status + ",返回信息：" + respBody);
            if (!"0".equals(status)) {
               throw new RuntimeException("TideDB返回异常,异常信息:" + respBody);
            }

            ((DirectBuffer)respHeadBuffer).cleaner().clean();
            return;
         }
      } catch (Exception var21) {
         logger.error(var21.getMessage(), var21);
         return;
      } finally {
         try {
            if (outputStream != null) {
               outputStream.close();
            }

            if (inputStream != null) {
               inputStream.close();
            }

            if (tideSocketClient != null) {
               tideSocketClient.close();
            }
         } catch (Exception var20) {
            outputStream = null;
            inputStream = null;
            tideSocketClient = null;
         }

      }

   }
}
